﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XNA_TD.Shells
{
    public abstract class SquarePointSprite : IRunable, IDraw
    {
        protected Vector3 position;
        VertexPositionTexture[] bulletVertices;
        static Effect effect = null;
        protected string technique;
        protected Vector4 color;
        protected float scale = 1;

        public SquarePointSprite(Vector3 pos, float scale, Vector4 color, string technique = "Square")
            : base(Priority.Effekt)
        {
            position = pos;
            bulletVertices = new VertexPositionTexture[6];

            Vector3 bottomLeft = new Vector3(-scale * .5f, -scale * .5f, 0);
            Vector3 bottomRight = new Vector3(scale * .5f, -scale * .5f, 0);
            Vector3 topLeft = new Vector3(-scale * .5f, scale * .5f, 0);
            Vector3 topRight = new Vector3(scale * .5f, scale * .5f, 0);

            int i = 0;
            bulletVertices[i++] = new VertexPositionTexture(topRight, new Vector2(1, 1));
            bulletVertices[i++] = new VertexPositionTexture(bottomLeft, new Vector2(0, 0));
            bulletVertices[i++] = new VertexPositionTexture(bottomRight, new Vector2(1, 0));

            bulletVertices[i++] = new VertexPositionTexture(topRight, new Vector2(1, 1));
            bulletVertices[i++] = new VertexPositionTexture(topLeft, new Vector2(0, 1));
            bulletVertices[i++] = new VertexPositionTexture(bottomLeft, new Vector2(0, 0));

            if (effect == null)
            {
                effect = Content.Load<Effect>("Dot");
            }
            this.technique = technique;
            this.color = color;
        }

        public void Draw()
        {
            effect.CurrentTechnique = effect.Techniques[technique];
            effect.Parameters["World"].SetValue(Matrix.CreateScale(scale) * Matrix.CreateBillboard(position, kamera.position, Matrix.Invert(viewMatrix).Up, null));
            effect.Parameters["View"].SetValue(viewMatrix);
            effect.Parameters["Projection"].SetValue(projectionMatrix);
            effect.Parameters["Color"].SetValue(color);


            graphics.GraphicsDevice.BlendState = BlendState.AlphaBlend;
            var old = graphics.GraphicsDevice.DepthStencilState;
            graphics.GraphicsDevice.DepthStencilState = DepthStencilState.None;
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                graphics.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, bulletVertices, 0, 2);
            }
            graphics.GraphicsDevice.DepthStencilState = old;
            graphics.GraphicsDevice.BlendState = BlendState.Opaque;
        }

        public void Explosion(float dmg, float maxDist)
        {
            var list = gameManager.getRenderList(Priority.Einheit);
            Dictionary<Unit, float> dmgList = new Dictionary<Unit, float>();
            foreach (var item in list)
            {
                Unit unit = item as Unit;
                if (unit != null)
                {
                    float dist = maxDist - (unit.position - position).Length();
                    if (dist > 0)
                    {
                        dmgList.Add(unit, dmg * dist);
                    }
                }
            }

            foreach (var item in dmgList)
            {
                item.Key.Damage(item.Value);
            }
        }

        public override void Load()
        {
        }

        public override void UnLoad()
        {
        }
    }
}
